package com.summer.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class NettyServer {
    public static void main(String[] args) throws InterruptedException {
        //1、创建线程池对象
        //bossGroup负责接收用户
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        //workGroup负责用户的io读写操作
        NioEventLoopGroup workGroup = new NioEventLoopGroup();
        //2、创建启动
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        //3、设置启动
        //添加到组中，参数1：负责接收  参数2：负责读写
        serverBootstrap.group(bossGroup,workGroup)
        .channel(NioServerSocketChannel.class)
        .childHandler(new ChannelInitializer<NioSocketChannel>() {
            protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                ChannelPipeline pipeline = nioSocketChannel.pipeline();
                //绑定编码
                pipeline.addFirst(new StringEncoder());
                pipeline.addLast(new StringDecoder());
                //绑定业务
                pipeline.addLast(new SimpleChannelInboundHandler<String>() {
                    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
                        //获取入栈信息
                        System.out.println(msg);
                    }
                });
            }
        });

        //4、启动引导
        ChannelFuture feature= serverBootstrap.bind(9999).sync();
        //5、关闭通道
        feature.channel().closeFuture().sync();
    }
}
